
import {
	getGroupListByOwnerID,
	getAvatarContentByID,
	getLiveStreamState,
	getAllADSTaskByGroupID
	} from "/request.js";
import { reactive } from 'vue';


var exportObject = reactive({
	token: "",
	userSelf: null
});
export default exportObject;


export var groupMap = reactive(new Map());
// groupMap.get(groupId).avatarContent
// groupMap.get(groupId).name
// groupMap.get(groupId).liveState


export var adsList = reactive([]);


export function updateGroupData()
{
	console.log("updateGroupData");
	if (!exportObject.userSelf)
	{
		console.log("未登录");
		return;
	}
	
	groupMap = reactive(new Map());
	getGroupListByOwnerID(exportObject.userSelf.id)
	.then(async function(list) {
		for (var i = 0; i < list.length; i++)
		{
			var g = list[i];
			var avatarContent;
			var liveState;
			await getAvatarContentByID(g.avatarId)
			.then(function(content) {
				avatarContent = content;
			});
			await getLiveStreamState(g.id)
			.then(function(state){
				liveState = state;
			});
			Object.assign(g, {
				avatarContent: avatarContent,
				liveState: liveState
			})
			groupMap.set(g.id, g);
		}
	});


}


/**
 * @param groupId {number} 群聊ID
 */
export function updateGroupLiveStateByGroupID(groupId)
{
	getLiveStreamState(groupId)
	.then(function(state) {
		groupMap.get(groupId).liveState = state;
	});
}


/**
 * @param groupId {number} 群聊ID
 */
export function updateADSDataByGroupID(groupId)
{
	while (adsList.length) {
		adsList.pop();
	}
	getAllADSTaskByGroupID(groupId)
	.then(function(list) {
		for (var item of list) {
			adsList.push(item);
		}
	});
}

